<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML>
<html style='height:100%'>
<head>
<base href="<%=basePath%>">

<title>信用管理</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link rel="stylesheet" href="css/vivo.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap.min.css">
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap-table.css">
<link rel="stylesheet" href="css/common.css">

<script src="js/jquery-1.11.1.js"></script>
<script src="js/bootstrap2/js/bootstrap.min.js"></script>
<script src="js/bootstrap2/js/bootstrap-table.js"></script>
<script src="js/bootstrap2/js/bootstrap-table-zh-CN.js"></script>
<script type="text/javascript" src="js/layui-v2.1.5/layui/layui.all.js"></script>
<script src="js/sharefunction.js"></script>
<script type="text/javascript" src="js/form.js"></script>
<script type="text/javascript">
var fkcustomerId,fkroomId;
var historyStatus=parent.parent.$("#historyStatus").val();//客户是否历史状态
	$(document).ready(function() {
		fkcustomerId = parent.parent.$("#customerId").val();
		fkroomId = parent.parent.$("#roomId").val();
		initCreditMsg();
		$(function() {
			mySubmit("creditForm", initCredit, successCredit);
		});
	});
	var ci;
	function initCreditMsg() {
		$.post("customer/credit/getCustomerCreditList", {
			fkcustomerId: fkcustomerId
			//fkroomId:fkroomId
		}, function(data, status) {
			if (data.length>0) {
				var html = "";
				for(var i=0; i < data.length;i++){
					var creditType = data[i].creditType;
					var content = data[i].content;
					var id = data[i].id;
					if(data[i].creditType=="司法纠纷"){
						var t = $("#sifa table");
						appendHtml(creditType,content,id,t);
					}else if(data[i].creditType=="矛盾投诉"){
						var t = $("#maodun table");
						appendHtml(creditType,content,id,t);
					}else if(data[i].creditType=="交易信息"){
						var t = $("#jiaoyi table");
						appendHtml(creditType,content,id,t);
					}else{
						var t = $("#other table");
						appendHtml(creditType,content,id,t);
					}
				}
				
				 //隐藏按钮
				$("#creditForm #btn_div2").show();
				$("#creditForm #btn_div1").hide();
				$("#creditForm").find("input,textarea").attr("readonly", true);
				$("#creditForm").find(".addbtn").hide();
				$("#creditForm").find(".icon-fullscreen").hide(); 
				
			}else{
			 	$("#creditForm #btn_div2").hide();
				$("#creditForm #btn_div1").show(); 
				$("#creditForm").find(".addbtn").show();
			}
			
			 if(historyStatus==1){
				//隐藏按钮
				$("#creditForm #btn_div2").hide();
				$("#creditForm #btn_div1").hide();
				$("#creditForm").find("input,textarea").attr("readonly", true);
				$("#creditForm").find(".addbtn").hide();
				$("#creditForm").find(".icon-fullscreen").hide();
			} 
			 var history = parent.parent.$("#history").val();
			if(history == 'yes'){
				//隐藏按钮
				$("#creditForm #btn_div2").hide();
				$("#creditForm #btn_div1").hide();
				$("#creditForm").find("input,textarea").attr("readonly", true);
				$("#creditForm").find(".addbtn").hide();
				$("#creditForm").find(".icon-fullscreen").hide();
				$("#modifyBtn").remove();
			}
		});
	}
	
	function appendHtml(creditType,content,id,t){
		html = "<tr><td width=\"73\"align=\"center\">记录</td><td><input name=\"creditType\"type=\"hidden\" value=\""+creditType+"\"/><input id=\"entityId_ids\" name=\"id\"type=\"hidden\" value=\""+id+"\"/><textarea name=\"content\"rows=\"3\"cols=\"70\"style=\"width:70%;\">"+content+"</textarea>"
		+"<a data-id=\""+id+"\" href=\"javascript:void(0)\"onclick=\"delTrCredit(this)\"><i class=\"icon-fullscreen\"></i></a></td></tr>"
		$(t).append(html);
	}
	
	var optCredit;
	//optCredit操作字段，0：临时保存；1：新增；2：修改
	function saveCredit(val) {
		optCredit = val;
		if (val == 3) {
			//解除锁定状态
			var index = layer.confirm('确定开启编辑状态？', {
				offset : '20%',
				shade : 0
			}, function() {
				//去除只读开启按钮编辑
				$("#creditForm").find("input,textarea").attr("readonly", false);
				$("#creditForm").find(".addbtn").show();
				$("#creditForm").find(".icon-fullscreen").show();
				$("#creditForm #modifyBtn").hide();
				$("#creditForm .hidbtn").addClass("showbtn");
				$("#creditForm #changeRemark").show();
				layer.close(index);
			});
		} else {

			$("#creditForm").submit();
		}
	}
	function initCredit() {
		ci = layer.load(2);
		var fkcustomerId = parent.parent.$("#customerId").val();
		var fkroomId = parent.parent.$("#roomId").val();
		var changeRemark = $("#creditForm [name='changeRemark']").val();
		var t1=$("#sifa tr:gt(0)");
		var t2=$("#maodun tr:gt(0)");
		var t4=$("#other tr:gt(0)");
		var arr1=new Array();
		var tableArr = [t1,t2,t4];
		for(var j=0;j<tableArr.length;j++){
			for(var i=0;i<tableArr[j].length;i++){
				var v=tableArr[j][i];
				var creditType=$(v).find("input[name='creditType']").val();
				var content=$(v).find("textarea[name='content']").val();
				var id=$(v).find("input[name='id']").val();
				arr1.push({creditType:creditType,content:content,id:id,fkcustomerId:fkcustomerId,fkroomId:fkroomId});
			}
		}
		if(optCredit=="2" && changeRemark==""){
			layer.msg("变更说明不能为空");
			$("#creditForm [name='changeRemark']").focus();
			layer.close(ci);
			return null;
		}

		
		return {
			delIds:delIds.toString(),
			creditList:arr1,
			opt:optCredit,
			changeRemark:changeRemark,
			fkcustomerId:fkcustomerId,
			fkroomId:fkroomId
		};
	}
	//成功回调
	function successCredit(responseText) {
		layer.close(ci);
		if (responseText.success) {
			layer.alert("保存成功！", {
				offset:'20%',
				shade:[0.1,'#fff']
			}, function() {
				parent.location.reload();
				layer.close(ci);
			});
		} else {
			layer.alert('保存失败！');
		}
	}
</script>
</head>
<body style='height:100%'>
	<form class="cmxform" id="creditForm" method="post"
		action="customer/credit/addCredit" onsubmit="return false;">
		<div>
			<div class="panel panel-default">
				<div class="panel-body">
					<!-- 司法纠纷 -->
					<div class="style_box3" id="sifa">
						<table cellpadding="10" cellspacing="5" class="mytable" id="lawnArea">
							<tr style="border-bottom:1px solid gray">
								<td width="73"  class="td-right"><span class="style_btn">司法纠纷</span>
								</td>
								<td><a class="btn btn-danger radius addbtn"
									 onclick="addTrCredit(this)" style="display: none"> 添加</a></td>
							</tr>
						</table>
					</div>
					<!-- 矛盾投诉-->
					<div class="style_box3" id="maodun">
						<table cellpadding="10" cellspacing="5" class="mytable" id="herbal" >
							<tr style="border-bottom:1px solid gray">
								<td width="73"><span class="style_btn">矛盾投诉</span>
								</td>
								<td><a class="btn btn-danger radius addbtn"
									 onclick="addTrCredit(this)" style="display: none"> 添加</a></td>
							</tr>
							
						</table>
					</div>
					<!-- 交易信息 -->
					<div class="style_box3" id="jiaoyi">
						<table cellpadding="10" cellspacing="5" class="mytable" id="woody">
							<tr style="border-bottom:1px solid gray">
								<td width="73"><span class="style_btn">交易信息</span>
								</td>
								<td><!-- <a class="btn btn-danger radius addbtn"
									 onclick="addTrCredit(this)"> 添加</a> --></td>
							</tr>
						</table>
					</div>
					<!-- 其他信用 -->
					<div class="style_box3" id="other">
						<table cellpadding="10" cellspacing="5" class="mytable" id="trees">
							<tr style="border-bottom:1px solid gray">
								<td width="73"><span class="style_btn">其他信用</span>
								</td>
								<td colspan="5"><a class="btn btn-danger radius addbtn"
									 onclick="addTrCredit(this)" style="display: none"> 添加</a></td>
							</tr>
						</table>
					</div>
				</div>
			</div>
			<div id="changeRemark" hidden="hidden">
			<span style="display: inline-block;width: 120px;text-align: center;">变更说明</span>
			<textarea rows="3" cols="70" style="width: 65%" name="changeRemark"></textarea><span style="color:red"> *</span>
			</div>
			<div style="width:100%;text-align:center; padding-top:20px">
				<div id="btn_div1" style="display:none;">
					<button class="btn btn-danger radius my-btn"
						onclick="saveCredit(0)" type="button">提交</button>
					<button class="btn btn-danger radius my-btn"
						onclick="saveCredit(1)" type="button">保 存</button>
					<a class="btn btn-default radius"
						onClick="resetCredit();"> 重置 </a> <br />
				</div>
				<div id="btn_div2" style="display:none;">
					<shiro:hasPermission name="permission:edit">
						 <c:if test="${requestScope.pendStatus }"><!-- 如果有待审批则不显示编辑按钮 --> 
							<button id="modifyBtn" type="button"
								class="btn btn-danger radius my-btn" onclick="saveCredit(3)">启用编辑</button>
							<button class="btn btn-danger radius my-btn hidbtn"
								onclick="saveCredit(2)" type="button">提交审批</button>
							<a class="btn btn-default radius hidbtn"
								onClick="resetCredit();">重置</a>
						 </c:if> 
					</shiro:hasPermission>
				</div>
			</div>

		</div>
	</form>
	<div style="padding: 10px" class="changeTable">
			<!-- 信用信息更改字段列表 -->
			<table id="creditChageTable" style="table-layout:fixed"></table>
		</div>
	<script type="text/javascript">
	$(function(){
		//修改列表展示
			//初始化表格,动态从服务器加载数据  
			 $("#creditChageTable").bootstrapTable({
				method : "post", //使用get请求到服务器获取数据  
				url : "customer/credit/getcreditChangeList", //获取数据的Servlet地址  
				striped : true, //表格显示条纹  
				pagination : true, //启动分页  
				pageSize : 2, //每页显示的记录数  
				pageNumber : 1, //当前第几页  
				pageList : [ 5, 10, 15, 20, 25 ], //记录数可选列表  
				sidePagination : "server", //表示服务端请求  
				contentType : 'application/x-www-form-urlencoded',
				//设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder  
				//设置为limit可以获取limit, offset, search, sort, order  
				queryParamsType : "undefined",
				queryParams : function queryParams(params) { //设置查询参数  
					var param = {
						pageNumber : params.pageNumber,
						pageSize : params.pageSize,
						fkcustomerId : fkcustomerId,
						fkroomId : fkroomId
					};
					return param;
				},
				columns : [ {
					field : 'createtime',
					title : '修改时间',
					width : '14%',
					formatter:creditDataFormat
				}, {
					field : 'fieldname',
					title : '修改字段',
					width : '8%'
				}, {
					field : 'changebefore',
					title : '修改前数据',
					width : '25%',
					formatter:showText	
				}, {
					field : 'changeafter',
					title : '修改后数据',
					width : '25%',
					formatter:showText	
				}, {
					field : 'adduser',
					title : '修改人',
					width : '7%',
				}, {
					field : 'remarks',
					title : '变更说明',
					width : '21%',
					formatter:showText	
				} ]
			}); 
	})
	//日期格式化
		function creditDataFormat(value, row, index) {
			return new Date(row.createtime).format("yyyy-MM-dd hh:mm:ss");
		}
		function resetCredit(){
			$("creditFormnput,textarea").val("");
			//$("#creditForm").find("select").val("司法纠纷").trigger("change");
		}
		
		function addTrCredit(obj) {
			var creditType = $(obj).parent().prev().text().trim();
			var html = "<tr><td width=\"73\"align=\"center\">记录</td><td><input name=\"creditType\"type=\"hidden\" value=\""+creditType+"\"/><input id=\"entityId_ids\" name=\"id\"type=\"hidden\"/><textarea name=\"content\"rows=\"3\"cols=\"70\"style=\"width:70%;\"></textarea>"
					+"<a href=\"javascript:void(0)\"onclick=\"delTrCredit(this)\"><i class=\"icon-fullscreen\"></i></a></td></tr>";
			$(obj).parent().parent().parent().append(html);
		}
		
		//存储将要删除的id，传到后台删除
		var delIds = new Array();
		function delTrCredit(obj) {
			var id = $(obj).attr("data-id");
			delIds.push(id);
			$(obj).parent().parent().remove();
			layer.msg("点击保存后，将彻底删除该条数据！");
		}
		
		/* function delTrCredit(obj){
			var id = $(obj).attr("data-id");
			layer.confirm('确定删除该条信息吗？', {
				btn : [ '确定', '取消' ] //按钮
			}, function() {
				//点击确定时，删除该条数据
				if (id== undefined || id == "") {
					$(obj).parent().parent().remove();
					layer.msg("删除成功");
				} else {
					 if(id!=null && id!=undefined && id != ""){
						$.getJSON("customer/credit/delCustomerCredit", {id:id}, function(data, status) {
							if (status == "success") {
								layer.msg("删除成功！");
								$(obj).parent().parent().remove();
							}
						});
					} 
				}
			}, function() {
				layer.closeAll();
			});
		} */
	</script>
</body>